<template>
  <div class="order">
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/home/welcome">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入订单"
            class="input-with-select"
            v-model="queryinfo.query"
            clearable
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表数据 -->
      <el-table :data="orderList" border style="width: 100%">
        <el-table-column type="index" label="#" width="100"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="380"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)" width="120"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay==1" type="success" size="normal" effect="dark">已付款</el-tag>
            <el-tag v-else type="danger" size="normal" effect="dark">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="120"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180">
          <template slot-scope="scope">{{scope.row.create_time|dateformat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="primary" icon="el-icon-edit" @click="showEdit"></el-button>
            <el-button size="small" type="success" icon="el-icon-setting" @click="showProgress"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 编辑的对话框 -->
      <el-dialog title="修改地址" :visible.sync="editFlag" width="50%" @close="resetForm">
        <el-form
          :model="editForm"
          :rules="editRules"
          ref="editForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="省市区\县" prop="address1">
            <el-cascader
              :options="cityData"
              v-model="editForm.address1"
              clearable
              :props="{ expandTrigger: 'hover', checkStrictly: true }"
              @change="addressChange"
              ref="cityData"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="editForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editFlag = false">取 消</el-button>
          <el-button type="primary" @click="editFlag = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 物流进度的对话框 -->
      <el-dialog title="物流进度" :visible.sync="goodFlag" width="50%">
        <el-timeline>
          <el-timeline-item
            v-for="(item, index) in progressList"
            :key="index"
            :timestamp="item.time"
          >{{item.context}}</el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="goodFlag = false">取 消</el-button>
          <el-button type="primary" @click="goodFlag = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  data () {
    return {
      // 查询信息
      queryinfo: {
        pagenum: 1,
        pagesize: 10,
        query: "",
      },
      // 编辑的表单信息
      editForm: {
        address1: [],
        address2: ""
      },
      // 物流数据信息
      progressList: [],
      // 验证规则
      editRules: {
        address1: [
          { required: true, message: '请输入地址名称', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请输入详细地址名称', trigger: 'blur' },
        ]
      },
      total: 0,
      // 订单数据
      orderList: [],
      // 编辑的对话框
      editFlag: false,
      // 物流进度的对话框
      goodFlag: false,
      // 城市数据
      cityData
    }
  },
  methods: {
    async getOrders () {
      const res = await this.axios.get('orders', { params: this.queryinfo });
      this.orderList = res.data.goods;
      this.total = res.data.total;
    },
    // 页码大小发生变化
    handleSizeChange (newS) {
      this.queryinfo.pagesize = newS;
      this.getOrders();
    },
    // 页码发生变化
    handleCurrentChange (newP) {
      this.queryinfo.pagenum = newP;
      this.getOrders();
    },
    showEdit () {
      this.editFlag = true;
    },
    // 关闭对话框  重置表单
    resetForm () {
      this.$refs.editForm.resetFields();
    },
    // 当cascade中的数据发生变化
    addressChange () {
      this.$refs.cityData.dropDownVisible = false;
    },
    // 物流对话框的操作  获取数据  并显示对话框
    async showProgress () {
      this.goodFlag = true;
      const res = await this.axios.get('/kuaidi/' + '1106975712662');
      this.progressList = res.data;
      console.log(res.data);

    }
  },
  created () {
    this.getOrders();
  },
}
</script>

<style lang="scss" >
.order {
  text-align: left;
  vertical-align: center;
  .el-form-item__label {
    margin-top: 8px;
  }
}
</style>